package ControlLayer;
import ModelLayer.*;
import java.util.ArrayList;
/**ControlLayer package sets the location for LoanController class.
 * 
 * Usually a 'Controller' is the interface between a user interface component
 * and a model (e.g. Register). Controllers should be thin classes, doing little
 * more than mapping user interface events to model functions.
 * 
 * The ModelLayer package is imported, because controllers must be able to access 
 * classes in the ModelLayer - AddressBook, DVDRegister and LoanRegister.
 * ---------------------------------------------------------------------------------
 * LoanController class is devided in 3 sections. In each section(except the first
 * one) methods with similar functionalities are placed. 
 *  
 *  Section Contents:
 *  Variables
 *  Constructors
 *      - LoanController()
 *  Methods
 *      - setLoanRegister()
 *      - findUnrentedCopy()
 *      - findPersonMIstake()
 *      - getLoanRegister()
 *      - deleteLoan()
 *      - findLoan()
 *      - editLoan()
 *      - returnLoan()
 *      - printLoans()
 */
public class LoanController
{
  /** VARIABLES */
    //LoanController has to store the processed information somewhere. For the 
    //purpose to provide a container for the controller, the following object
    //reference(association) is needed.
    private LoanRegister loanRegister;
    private DvdRegister register;
    private AddressBook addressBook;

  /** CONSTRUCTOR */
  public LoanController()
  {
      loanRegister = LoanRegister.getInstance();
      addressBook = AddressBook.getInstance();
      register = DvdRegister.getInstance();
      //Sets the containers for storing items.
    }
    
  /** METHODS */
  public void setLoanRegister(int id, String borrowDate, int period,String dvdTitle, String personID)
  //Sets a DVD copy to status rented.
  {
      Copy myCopy = register.findDVD(dvdTitle).findUnrentedCopy();
      loanRegister.setLoan(id, borrowDate, period, myCopy, addressBook.findPerson(personID));
  }
  
  public Copy findUnrentedCopy(String dvdTitle)
  //Finds an unrented DVD copy.
  {
      return register.findDVD(dvdTitle).findUnrentedCopy();
  }

  public Person findPersonMIstake(String personID)
  //Finds a person by ID.
  {
      return addressBook.findPerson(personID);
  }
  
  public ArrayList<Loan> getLoanRegister()
  //Displays all loans in the register.
  {
      return loanRegister.getLoan();
  }
  
  public void deleteLoan(int id)
  //Delete a loan, sets a DVD copy status to returned.
  {
      loanRegister.deleteLoan(id);
  }
   
  public Loan findLoan(int id)
  //Finds a loan by id.
  {
      return loanRegister.findLoan(id);
  }
  
  public void editLoan(int id, String borrowDate, int period)
  //Updates loan information by id.
  {
      loanRegister.editLoan(findLoan(id),  borrowDate, period);
  }
    
  public String returnLoan(int id,int howMuchWasRented)
  //Deletes a loan by id.
  {
      boolean t = false;
      if(howMuchWasRented>findLoan(id).getPeriod())
      {
        t=true;
      }
      String message ="";
      //Loan loan1=findLoan(id);
      findLoan(id).getPerson().deleteLoan(findLoan(id));
      findLoan(id).getCopy().unrentCopy();
      findLoan(id).getCopy().deleteLoan();
      loanRegister.deleteLoan(findLoan(id));
      if(t)
      {
          message = "Extra money has to be paid"+'\n';
      }
      else
      {
          message = "The DVD was returned in time"+ '\n';
      }
        return message;
  }
    
  public String printLoans()
  //Shows all loans in the register.
  {
      return loanRegister.printLoans();
  }
}